import axios from 'axios'

let http = axios.create({
    timeout: 60000
})

http.defaults.headers.post['Content-Type'] = 'application/json'
http.defaults.baseURL = ''

http.interceptors.request.use(config => {
    if (config.method === 'post') {
        // config.data = JSON.stringify(config.data)
    }
    return config
}, error => {
    return Promise.reject(error)
})

http.interceptors.response.use(response => {
    let {data} = response
    if (response.config.url == '/dlapi/attach/download' || response.config.url == '/dlapi/device/reportZip') {
        return response
    } else {
        return data
    }
}, error => {
    let info = {}
    let {status, statusText, data} = error.response
    if (!error.response) {
        info = {
            code: 5000,
            msg: 'Network Error'
        }
    } else {
        info = {
            code: status,
            data: data,
            msg: statusText
        }
    }
    return Promise.reject(info)
})

export default function() {
    return http
}
